package com.example.assisteddriving;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.assisteddriving.bean.Xinlvlog;
import com.example.assisteddriving.dao.DrivelogDao;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;

public class Tongji extends Fragment {
    View view;
    private LineChart lineChart,lineChart2,lineChart3;
    final List<Entry> entries = new ArrayList<Entry>();
    final List<Entry> entries2 = new ArrayList<Entry>();
    final List<Entry> entries3 = new ArrayList<Entry>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.tongji,null);
        Log.v("asd","cr");
        inittable1();
        inittable2();
        inittable3();
        return view;
    }
    @Override
    public void onResume() {
        super.onResume();
        Log.v("asd","re");
    }
    @Override
    public void onPause() {
        super.onPause();
        Log.v("asd","pa");
    }
    public void inittable1() {
        DrivelogDao drivelogDao = new DrivelogDao(getContext());
        List<Xinlvlog> xinlvlogs = drivelogDao.getxl();
        if (xinlvlogs != null) {
            if (xinlvlogs.size() > 10) {
                int i=0;
                for (int j = xinlvlogs.size()-1; j > xinlvlogs.size() - 11; j--) {

                    float xl = Float.parseFloat(xinlvlogs.get(j).getXinlv());
                    entries.add(new Entry(i, xl));
                    i=i+1;
                }
            }else {
                for (int i = 0; i < xinlvlogs.size(); i++) {
                    float xl = Float.parseFloat(xinlvlogs.get(i).getXinlv());
                    entries.add(new Entry(i, xl));
                }
            }

        }else{
            for (int i = 0; i < 10; i++) {
                float xl = 0;
                entries.add(new Entry(i, xl));
            }
        }
            lineChart = (LineChart) view.findViewById(R.id.chart);
            LineDataSet dataSet = new LineDataSet(entries, "心率");
            dataSet.setColor(Color.parseColor("#08FF00"));
            dataSet.setCircleColor(Color.parseColor("#000000"));
            dataSet.setCircleRadius(3f);
            dataSet.setLineWidth(1.5f);
            dataSet.setValueTextColor(Color.parseColor("#000000"));
            dataSet.setValueTextSize(9f);
            YAxis rightAxis = lineChart.getAxisRight();
            rightAxis.setEnabled(false);
            XAxis xAxis = lineChart.getXAxis();
            xAxis.setTextColor(Color.BLACK);
            xAxis.setDrawAxisLine(true);
            xAxis.setDrawGridLines(true);
            xAxis.setDrawLabels(true);
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float v, AxisBase axis) {
                    return String.valueOf((int) v + 1).concat("");
                }
            });
            Legend legend = lineChart.getLegend();
            legend.setForm(Legend.LegendForm.LINE);
            legend.setTextSize(15f);
            legend.setFormSize(15f);
            legend.setTextColor(Color.BLUE);
            Description description = lineChart.getDescription();
            description.setEnabled(false);
            LineData lineData = new LineData(dataSet);
            lineChart.setTouchEnabled(false);
            lineChart.setData(lineData);
            lineChart.invalidate();

    }
    private void inittable2() {
        DrivelogDao drivelogDao=new DrivelogDao(getContext());
        List<Xinlvlog> xinlvlogs=drivelogDao.getxl();
        if (xinlvlogs != null) {
            if (xinlvlogs.size() > 10) {
                int i=0;
                for (int j = xinlvlogs.size()-1; j > xinlvlogs.size() - 11; j--) {

                    float xl = Float.parseFloat(xinlvlogs.get(j).getTiwen());
                    entries2.add(new Entry(i, xl));
                    i=i+1;
                }
            }else {
                for (int i = 0; i < xinlvlogs.size(); i++) {
                    float tw = Float.parseFloat(xinlvlogs.get(i).getTiwen());
                    entries2.add(new Entry(i, tw));
                }
            }
        }else {
            for (int i = 0; i < 10; i++) {
                float tw = 0;
                entries2.add(new Entry(i, tw));
            }
        }
            lineChart2 = (LineChart) view.findViewById(R.id.chart2);
            LineDataSet dataSet = new LineDataSet(entries2, "体温");
            dataSet.setColor(Color.parseColor("#FF1600"));
            dataSet.setCircleColor(Color.parseColor("#000000"));
            dataSet.setCircleRadius(3f);
            dataSet.setLineWidth(1.5f);
            dataSet.setValueTextColor(Color.parseColor("#000000"));
            dataSet.setValueTextSize(9f);
            YAxis rightAxis = lineChart2.getAxisRight();
            rightAxis.setEnabled(false);
            XAxis xAxis = lineChart2.getXAxis();
            xAxis.setTextColor(Color.BLACK);
            xAxis.setDrawAxisLine(true);
            xAxis.setDrawGridLines(true);
            xAxis.setDrawLabels(true);
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float v, AxisBase axis) {
                    return String.valueOf((int) v + 1).concat("");
                }
            });
            Legend legend = lineChart2.getLegend();
            legend.setForm(Legend.LegendForm.LINE);
            legend.setTextSize(15f);
            legend.setFormSize(15f);
            legend.setTextColor(Color.BLUE);
            Description description = lineChart2.getDescription();
            description.setEnabled(false);
            LineData lineData = new LineData(dataSet);
            lineChart2.setTouchEnabled(false);
            lineChart2.setData(lineData);
            lineChart2.invalidate();

    }

    private void inittable3() {
        DrivelogDao drivelogDao=new DrivelogDao(getContext());
        List<Xinlvlog> xinlvlogs=drivelogDao.getxl();
        if (xinlvlogs != null) {
            if (xinlvlogs.size() > 10) {
                int i=0;
                for (int j = xinlvlogs.size()-1; j > xinlvlogs.size() - 11; j--) {

                    float xl = Float.parseFloat(xinlvlogs.get(j).getXueyang());
                    entries3.add(new Entry(i, xl));
                    i=i+1;
                }
            }else {
                for (int i = 0; i < xinlvlogs.size(); i++) {
                    float xl = Float.parseFloat(xinlvlogs.get(i).getXueyang());
                    entries3.add(new Entry(i, xl));
                }
            }
        }else {
            for (int i = 0; i < 10; i++) {
                float xy = 0;
                entries3.add(new Entry(i, xy));
            }
        }
            lineChart3 = (LineChart) view.findViewById(R.id.chart3);
            LineDataSet dataSet = new LineDataSet(entries3, "血氧");
            dataSet.setColor(Color.parseColor("#00C4FF"));
            dataSet.setCircleColor(Color.parseColor("#000000"));
            dataSet.setCircleRadius(3f);
            dataSet.setLineWidth(1.5f);
            dataSet.setValueTextColor(Color.parseColor("#000000"));
            dataSet.setValueTextSize(9f);
            YAxis rightAxis = lineChart3.getAxisRight();
            rightAxis.setEnabled(false);
            XAxis xAxis = lineChart3.getXAxis();
            xAxis.setTextColor(Color.BLACK);
            xAxis.setDrawAxisLine(true);
            xAxis.setDrawGridLines(true);
            xAxis.setDrawLabels(true);
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float v, AxisBase axis) {
                    return String.valueOf((int) v + 1).concat("");
                }
            });
            Legend legend = lineChart3.getLegend();
            legend.setForm(Legend.LegendForm.LINE);
            legend.setTextSize(15f);
            legend.setFormSize(15f);
            legend.setTextColor(Color.BLUE);
            Description description = lineChart3.getDescription();
            description.setEnabled(false);
            LineData lineData = new LineData(dataSet);
            lineChart3.setTouchEnabled(false);
            lineChart3.setData(lineData);
            lineChart3.invalidate();

    }


}
